<?php
// +----------------------------------------------------------------------
// | B5LaravelCMF
// +----------------------------------------------------------------------
// | Author: 李恒 <357145480@qq.com>
// +----------------------------------------------------------------------
namespace App\Validates;


use App\Services\DictTypeService;

class DictTypeValidate extends ValidateBase
{
    public function rules()
    {
        return [
            'name' => 'required|min:2|max:50',
            'type' => 'required|min:3|max:50|alpha_dash',
            'listsort' => 'integer'
        ];
    }

    public function attributes()
    {
        return [
            'name' => '字典名称',
            'type' => '字典标识',
            'listsort' => '显示顺序'
        ];
    }

    /**
     * 验证标识的唯一性
     * @return ValidateBase
     */
    protected function after_validate()
    {
        if (empty($this->error)) {
            if ($this->data['type']) {
                $service = new DictTypeService();
                $expect = $this->type == 'edit' ? [$this->data['id']] : [];
                $exist = $service->exist(['type' => trim($this->data['type'])], $expect);
                if ($exist) {
                    $this->error = '字典标识已存在';
                }
            }
        }
        return parent::after_validate(); // TODO: Change the autogenerated stub
    }
}
